package com.iot08.common.utils;

import org.apache.tomcat.jni.Local;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;

import java.util.Locale;

/**
 * @author weichao 1299101858@qq.com
 * @version 1.0
 * @date 1/18/22 4:18 PM
 */
public class MessageUtils {
    private MessageUtils() { throw new IllegalStateException("Utility class"); }

    private static final MessageSource messageSource = SpringUtil.getBean(MessageSource.class);

    public static String getLocale(String msg) {

        return getLocale(LocaleContextHolder.getLocale(), msg);

    }

    public static String getLocale(String msg, Object... args) {
        return getLocale(LocaleContextHolder.getLocale(), msg, args);
    }

    public static String getLocale(Locale locale, String msg, Object... args) {
        String tempMsg;
        try {
            tempMsg = messageSource.getMessage(msg, null, locale);
        } catch (Exception e) {
            e.printStackTrace();
            tempMsg = msg;
        }
        tempMsg = String.format(tempMsg, args);
        return tempMsg;
    }

//    public static void main(String[] args){
//        String localeStr = MessageUtils.getLocale("%s aaaaa %s", "cccc", "nnnn");
//        System.out.println(String.format("localeStr:%s", localeStr));
//    }

}
